package br.ufsc.ctc.ine5622.compilador.gui;

import java.awt.Dimension;

import java.awt.Frame;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JScrollPane;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;

/**
 * This class will launch the help contents dialog when
 * instantiated.
 * 
 * @author Paulo Pinho
 * @author William di Domenico
 * 
 */

public class DialogAjuda extends JDialog implements HyperlinkListener {
	private static final long serialVersionUID = 1L;

	private JEditorPane editorPane = new JEditorPane();
	private URL helpUrl;

	public DialogAjuda(Frame owner) {
		super(owner, "Ajuda do compilador");
		composeGui();
	}

	/**
	 * Handles the creation and composition of GUI components.
	 */
	public void composeGui() {
		try {
			editorPane.addHyperlinkListener(this);
			editorPane.setEditable(false);
			File f = new File("");
			helpUrl = new URL("file:" + f.getAbsolutePath() + File.separator
					+ "resources" + File.separator + "ajuda.html");

			editorPane.setPage(helpUrl);

			// Put the editor pane in a scroll pane.
			JScrollPane editorScrollPane = new JScrollPane(editorPane);
			editorScrollPane
					.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
			editorScrollPane.setPreferredSize(new Dimension((int) (Toolkit
					.getDefaultToolkit().getScreenSize().getWidth() / 2.5),
					(int) (Toolkit.getDefaultToolkit().getScreenSize()
							.getWidth() / 2)));
			editorScrollPane.setMinimumSize(new Dimension(250, 145));

			this.add(editorScrollPane);
			this.pack();
			this.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
			this.setVisible(true);
		} catch (MalformedURLException e) {

		} catch (IOException e) {

		}
	}

	/**
	 * Handles clicks in the hyperlinks, loading 
	 * them in the default system browser.
	 */
	@Override
	public void hyperlinkUpdate(HyperlinkEvent e) {
		if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
			try {
				editorPane.setPage(e.getURL());
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		}
	}

}
